﻿using FreeSql;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Org.BouncyCastle.Bcpg;
using ManaMaple.DB.IRepository;
using ManaMaple.DB.Entity;
using ManaMaple.DB.Models;
using ManaMaple.Kit.Helpers;
using ManaMaple.Kit.Models;

namespace ManaMaple.DB.Repository
{
    /// <summary>
    /// 描述：公用仓储(int)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class FoundRepository<T> : BaseRepository<T, int>, IFoundRepository<T> where T : BaseEntity, new()
    {
        /// <summary>
        /// 用户信息
        /// </summary>
        protected  CurrentUserModel CurrenUserInfo;
        public FoundRepository(IFreeSql fsql, UnitOfWorkManager unitOfWork) : base(unitOfWork?.Orm ?? fsql)
        {
            unitOfWork?.Binding(this);
        }

        #region 单表操作
        /// <summary>
        /// ID获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual async Task<T> GetByIdAsync(int id)
        {
            return await base.Select.Where(a => a.Id == id).FirstAsync();
        }
        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public virtual async Task<IList<T>> GetListAsync()
        {
            return await base.Select.ToListAsync();
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="select">连接查询</param>
        /// <param name="Page">分页条件</param>
        /// <returns></returns>
        public virtual async Task<PageReturn<T>> GetPageListAsync(ISelect<T> select, PageSearch Page)
        {
            PageReturn<T> search_list = new PageReturn<T>();
            long totalCount = 0;
            search_list.DataList = await select.Count(out totalCount).OrderBy(string.IsNullOrWhiteSpace(Page.OrderBy) ? "" : Page.OrderBy + " " + Page.Direction).Page(Page.PageIndex, Page.PageSize).ToListAsync();
            search_list.TotalCount = totalCount.ObjToInt();
            search_list.PageSize = Page.PageSize;
            search_list.PageIndex = Page.PageIndex;
            return search_list;
        }
        /// <summary>
        /// 主键删除(假)
        /// </summary>
        /// <param name="t">必须是带Stt列的数据类</param>
        /// <returns></returns>
        public virtual async Task<bool> FalseDelete(int id)
        {
            var upd = UpdateDiy.Set(a => a.Uptr, CurrenUserInfo?.UserID).Set(a => a.UptrName, CurrenUserInfo?.UserName);
            //upd = UpdateDiy.Set(t => new BaseEntity { Uptr = UserInfo.UserID, UptrName = UserInfo.UserName });
            return await upd.SetRaw("is_del = ?is_del,UptDtt = ?UptDtt", new { is_del = 1}).Where(w => w.Id == id).ExecuteAffrowsAsync() > 0;
        }
        /// <summary>
        /// 主键删除(真)
        /// </summary>
        /// <param name="t">数据</param>
        /// <returns></returns>
        public virtual async Task<bool> TrueDeleteAsync(T t)
        {
            return await base.DeleteAsync(t) > 0;
        }
        #endregion

        #region Sql和存储过程操作
        /// <summary>
        /// Sql查询
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns></returns>
        public virtual async Task<IList<T1>> SqlQueryAsync<T1>(string sql) where T1 : class
        {
            return await Orm.Select<T1>().WithSql(sql).ToListAsync();
        }
        /// <summary>
        /// Sql查询(返回DataTable)
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns></returns>
        public virtual async Task<DataTable> SqlQueryDt<T1>(string sql) where T1 : class
        {
            return await Orm.Select<T1>().WithSql(sql).ToDataTableAsync();
        }
        /// <summary>
        /// Sql查询 分页
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="Page">分页</param>
        /// <returns></returns>
        public virtual async Task<PageReturn<T1>> SqlQueryPage<T1>(string sql, PageSearch Page) where T1 : class
        {
            PageReturn<T1> search_list = new PageReturn<T1>();
            long totalCount = 0;
            search_list.DataList = await Orm.Select<T1>().WithSql(sql).Count(out totalCount).OrderBy(string.IsNullOrWhiteSpace(Page.OrderBy) ? "" : Page.OrderBy + " " + Page.Direction).Page(Page.PageIndex, Page.PageSize).ToListAsync();
            search_list.TotalCount = totalCount.ObjToInt();
            search_list.PageSize = Page.PageSize;
            search_list.PageIndex = Page.PageIndex;
            return search_list;
        }
        /// <summary>
        /// Sql查询 分页(返回DataTable)
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="Page"></param>
        /// <returns></returns>
        public virtual async Task<PageReturn<T1>> SqlQueryPageDt<T1>(string sql, PageSearch Page) where T1 : class
        {
            PageReturn<T1> search_list = new PageReturn<T1>();
            long totalCount = 0;
            search_list.SumData = await Orm.Select<T1>().WithSql(sql).Count(out totalCount).OrderBy(string.IsNullOrWhiteSpace(Page.OrderBy) ? "" : Page.OrderBy + " " + Page.Direction).Page(Page.PageIndex, Page.PageSize).ToDataTableAsync();
            search_list.TotalCount = totalCount.ObjToInt();
            search_list.PageSize = Page.PageSize;
            search_list.PageIndex = Page.PageIndex;
            return search_list;
        }
        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <param name="name">存储过程名称</param>
        public virtual void Procedure(string name)
        {
            ////var nameP = new SugarParameter("@name", "张三");
            ////var ageP = new SugarParameter("@age", null, true);//isOutput=true
            ////ageP.value可以拿到返回值
            //var dt2 = Db.Ado.UseStoredProcedure().GetDataTable(name, sugar);
        }
        #endregion
    }
}
